{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aa5099ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "844d61bb",
   "metadata": {},
   "source": [
    "作业：\n",
    "\n",
    "1.分别随机生成六个班的考试成绩，每个班50人，考试共三门：Python、数学、语文。每个班级成绩的shape应当是50x3\n",
    "\n",
    "2.将这六个班的考试成绩垂直叠加得到二维数组score。(即score的每一行是一个同学的成绩)\n",
    "\n",
    "3.生成性别数组sex，水平叠加数组sex和score得到data\n",
    "\n",
    "4.分性别计算各科成绩的统计指标：最小值、最大值、平均分、中位数、标准差\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9f5a8ab5",
   "metadata": {},
   "outputs": [],
   "source": [
    "class1 = np.random.randint(0,150,size=(50,3))\n",
    "class2 = np.random.randint(0,150,size=(50,3))\n",
    "class3 = np.random.randint(0,150,size=(50,3))\n",
    "class4 = np.random.randint(0,150,size=(50,3))\n",
    "class5 = np.random.randint(0,150,size=(50,3))\n",
    "class6 = np.random.randint(0,150,size=(50,3))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d535b250",
   "metadata": {},
   "outputs": [],
   "source": [
    "score= np.vstack((class1,class2,class3,class4,class5,class6))\n",
    "# c = np.vstack((class1,class2,class3,class4,class5,class6)\n",
    "# c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "78f17fda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(300, 3)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6339f4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "##男用0表示，女用1表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c1d76446",
   "metadata": {},
   "outputs": [],
   "source": [
    "sex = np.random.randint(0,2,size=(300,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "437e2011",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.hstack( (sex, score) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ee070612",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0,   0,  22, 128],\n",
       "       [  0,   0,  30, 130],\n",
       "       [  0,   0,  52,  75],\n",
       "       ...,\n",
       "       [  1, 101, 109, 143],\n",
       "       [  1, 108, 110, 141],\n",
       "       [  1, 112, 137, 142]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#lexsort默认按最后一行元素有小到大排序, 返回最后一行元素排序后索引所在位置\n",
    "data2 = data[np.lexsort(data[:,::-1].T)]\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "afee1610",
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.where()[0]+1\n",
    "# np.where(np.diff(data2[:,0]))\n",
    "#data2是根据第一列排序后的数组，根据第一列的值，将数组一份为2，就要找出从哪个位置开始分\n",
    "data_split=np.split(data2, np.where(np.diff(data2[:,0]))[0]+1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "57d26a38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  6, 27])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩最小值\n",
    "data_split[1][:,1:].min(axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "6b41d4c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([112, 139, 149])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩最大值\n",
    "data_split[1][:,1:].max(axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "bf948a2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 36.41509434,  69.81132075, 110.93081761])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩平均分\n",
    "data_split[1][:,1:].mean(axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "9ad575bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 31.,  67., 116.])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩中位数\n",
    "np.median(data_split[1][:,1:],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "30c645e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([26.47564489, 34.06249709, 27.82924562])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#女生各科成绩标准差\n",
    "data_split[1][:,1:].std(axis=0) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "487d8f61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  2, 20])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩最小值\n",
    "data_split[0][:,1:].min(axis=0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "bf30019f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([123, 145, 149])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩最大值\n",
    "data_split[0][:,1:].max(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "dd302d8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 35.66666667,  76.67375887, 113.70921986])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩平均分\n",
    "data_split[0][:,1:].mean(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "048b737d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 28.,  76., 121.])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩中位数\n",
    "np.median(data_split[0][:,1:],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "f94a3cfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([30.05849302, 33.86613121, 29.0540067 ])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#男生各科成绩标准差\n",
    "data_split[0][:,1:].std(axis=0) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f01df14f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
